package com.ruoyi.common.utils.baidu;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.springframework.core.io.ClassPathResource;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import java.io.*;

public class ExecuJs {
    public static void main(String[] args) throws Exception {

        // 获取JS执行引擎
        ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript");

        String a = "sleep", b = "320305.131321201";

        System.out.println("输入的参数【" + a + "】 + 【" + b + "】");

        getBaiduFanYiExecResult(a, b);

    }

    public static  <T> T getBaiduFanYiExecResult(String query, String gtk) {
        try {
            // return getExecResult("/home/bukaa/IdeaProjects/kydms/backend/src/main/resources/templates/baidu-fanyi.js", "e", query, gtk);
            ;
//            return getExecResult(System.getProperty("user.dir")+"/task/src/main/resources/templates/baidu-fanyi.js", "e", query, gtk);
            try{
                InputStream inputStream = new ClassPathResource("classpath:templates/baidu-fanyi.js").getInputStream();
                Reader reader = new InputStreamReader(inputStream);
                return getExecResult(reader, "e", query, gtk);
            }catch (Exception e){}
            return getExecResult(System.getProperty("user.dir")+"/ruoyi-admin/src/main/resources/templates/baidu-fanyi.js", "e", query, gtk);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static  <T> T getExecResult(String filePath, String fun, String ...params) throws FileNotFoundException, ScriptException, NoSuchMethodException {
        ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript");

        // String a = "sleep", b = "320305.131321201";
        se.eval(new FileReader(filePath));
        // 是否可调用
        if (se instanceof Invocable) {
            Invocable in = (Invocable) se;
            T result = (T) in.invokeFunction(fun, params);
            // System.out.println("获得的结果：" + result);
            return result;
        }
        return null;
    }
    public static ScriptObjectMirror getExecResult(String script) throws ScriptException {
        ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript");
        return (ScriptObjectMirror) se.eval(script);
    }

    public static  <T> T getExecResult(Reader reader, String fun, String ...params) throws FileNotFoundException, ScriptException, NoSuchMethodException {
        ScriptEngine se = new ScriptEngineManager().getEngineByName("javascript");

        // String a = "sleep", b = "320305.131321201";
        se.eval(reader);
        // 是否可调用
        if (se instanceof Invocable) {
            Invocable in = (Invocable) se;
            T result = (T) in.invokeFunction(fun, params);
            // System.out.println("获得的结果：" + result);
            return result;
        }
        return null;
    }
}